package tk.mybatis.springboot.share.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class PropertiesUtil {
	private static final Logger log = LogManager.getLogger(PropertiesUtil.class);

	/** 全局的properties */
	public static Map<String, String> baseProperties = new HashMap<String, String>();

	/**
	 * 根据路径返回所有的键值对
	 * @param urls 一个或多个文件路径
	 * @return
	 */
	public static Map<String, String> getProperties(String... urls) {
		Map<String, String> map = new HashMap<String, String>();

		Properties pro = new Properties();
		if (null != urls && urls.length > 0) {
			for (String str : urls) {
				InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(str);
				try {
					if (in == null) {
						continue;
					}
					pro.load(in);
					for (Object key : pro.keySet()) {
						String value = new String(pro.getProperty((String) key).toString().getBytes(), "UTF-8");
						map.put(key.toString(), value);
					}
				} catch (Exception e) {
					log.info("properties文件:" + str + "读取错误");
					e.printStackTrace();
					continue;
				}

			}
		}
		return map;
	}
	
	public static String getMessage(String code){
		return baseProperties.get(code);
	}
}
